using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
using Voodoo.Pattern;
using Voodoo.Utils;

namespace Voodoo.Meta.RVModules
{
	public class ChestRoomAdapter : MonoBehaviour
	{
		[SerializeField]
		private CanvasGroup canvasGroup;

		[SerializeField]
		private ChestRoomView chestRoomView;

		[SerializeField]
		private GameObject rvButton;

		[SerializeField]
		private GameObject noThanksButton;

		[SerializeField]
		private List<RewardInfo> defaultRewards;

		[SerializeField]
		private RewardInfo defaultBestReward;

		[SerializeField]
		private AudioClip bigGemSound;

		private RewardInfo _bestReward;

		private Action _callback;

		private List<RewardInfo> _rewardsLeft = new List<RewardInfo>();

		private CancellationTokenSource _cancellationTokenSource;

		private static ChestRoomAdapter _instance;

		public RewardInfo BestReward => _bestReward;

		public static ChestRoomAdapter Instance
		{
			get
			{
				if (!_instance)
				{
					_instance = UnityEngine.Object.FindObjectOfType<ChestRoomAdapter>();
				}
				return _instance;
			}
		}

		private void Awake()
		{
			if (_instance == null)
			{
				_instance = this;
			}
			else if (_instance != this)
			{
				UnityEngine.Object.Destroy(base.gameObject);
				return;
			}
			UnityEngine.Object.DontDestroyOnLoad(base.gameObject);
			HideCanvasGroup();
		}

		private void HideCanvasGroup()
		{
			canvasGroup.alpha = 0f;
			canvasGroup.interactable = false;
			base.gameObject.SetActive(value: false);
		}

		private void OnDestroy()
		{
			_cancellationTokenSource?.Cancel();
		}

		private void InitRewards()
		{
			_bestReward = defaultBestReward;
			_rewardsLeft = SingletonMB<ChestRoomRewardsController>.Instance.GetRewards();
			_rewardsLeft.Add(_bestReward);
			_rewardsLeft.Shuffle();
		}

		public void Show(Action onClose)
		{
			SingletonMB<UIManager>.Instance.ShowMainUI();
			_callback = onClose;
			InitRewards();
			base.gameObject.SetActive(value: true);
			canvasGroup.interactable = true;
			FadeIn(1f, 0.5f);
			rvButton.SetActive(value: false);
			noThanksButton.SetActive(value: false);
			chestRoomView.OnOpen();
		}

		private async void FadeIn(float endValue, float duration)
		{
			_cancellationTokenSource = new CancellationTokenSource();
			float baseAlpha = canvasGroup.alpha;
			float timeElapsed = 0f;
			while (timeElapsed < duration)
			{
				canvasGroup.alpha = Mathf.Lerp(baseAlpha, endValue, timeElapsed / duration);
				await Task.Yield();
				timeElapsed += Time.deltaTime;
				if (canvasGroup == null || _cancellationTokenSource.IsCancellationRequested)
				{
					break;
				}
			}
		}

		public async Task ShowButtons()
		{
			rvButton.SetActive(value: true);
			noThanksButton.SetActive(value: false);
			_cancellationTokenSource = new CancellationTokenSource();
			await Task.Delay(TimeSpan.FromSeconds(1.5), _cancellationTokenSource.Token);
			noThanksButton.SetActive(value: true);
		}

		public void HideButtons()
		{
			_cancellationTokenSource?.Cancel();
			rvButton.SetActive(value: false);
			noThanksButton.SetActive(value: false);
		}

		public void Hide()
		{
			_cancellationTokenSource?.Cancel();
			HideCanvasGroup();
			_callback?.Invoke();
			SingletonMB<UIManager>.Instance.HideMainUI();
		}

		public void OnNoThanksClicked()
		{
			Hide();
		}

		public void OnRVClicked()
		{
			OnComplete(success: true);
		}

		private void OnComplete(bool success)
		{
			if (success)
			{
				HideButtons();
				chestRoomView.FillUpKeys();
			}
		}

		public RewardInfo NextReward()
		{
			if (_rewardsLeft.Count != 0)
			{
				return _rewardsLeft[0];
			}
			return null;
		}

		public async Task CollectReward()
		{
			RewardInfo reward = _rewardsLeft[0];
			AddCurrency(reward);
			_rewardsLeft.RemoveAt(0);
			chestRoomView.OnChestOpened();
			_cancellationTokenSource = new CancellationTokenSource();
			await Task.Delay(TimeSpan.FromSeconds(0.10000000149011612), _cancellationTokenSource.Token);
			PlaySound(bigGemSound);
		}

		private void AddCurrency(RewardInfo reward)
		{
			if (reward.rewardType == RewardType.Cash)
			{
				NintendoPlayerPrefs.SetFloat("Money", NintendoPlayerPrefs.GetFloat("Money", 0f) + (float)reward.amount);
			}
			if (reward.rewardType == RewardType.Oil)
			{
				NintendoPlayerPrefs.SetInt("Oil", NintendoPlayerPrefs.GetInt("Oil", 0) + reward.amount);
			}
			if (reward.rewardType == RewardType.Length)
			{
				NintendoPlayerPrefs.SetInt("Long", NintendoPlayerPrefs.GetInt("Long", 0) + reward.amount);
			}
			if (reward.rewardType == RewardType.Power)
			{
				NintendoPlayerPrefs.SetInt("Power", NintendoPlayerPrefs.GetInt("Power", 0) + reward.amount);
			}
			if (reward.rewardType == RewardType.Size)
			{
				NintendoPlayerPrefs.SetInt("Size", NintendoPlayerPrefs.GetInt("Size", 0) + reward.amount);
			}
			SingletonMB<Shop>.Instance.CheckButtons();
		}

		public static bool IsInAB()
		{
			return Preloader.ChestRoomConfig.IsEnabled;
		}

		public static void DoVibrateMedium()
		{
			SingletonMB<GameManager>.Instance.GameplayHaptic.PlayHaptic(HapticType.Small);
		}

		public static int GetCurrentLevel()
		{
			return 0;
		}

		public static void PlaySound(AudioClip clip)
		{
			SingletonMB<GameManager>.Instance.GameplayAudio.PlaySound(clip);
		}
	}
}
